@extends('admin.public.base')
<style>
    table th {
        min-width: 10px !important;
    }
    .has-permission {
        background-color: #f0f9eb;
    }
    .no-permission {
        background-color: #fef0f0;
    }
</style>
@section('content')
    <div class="x-nav">
        <span class="layui-breadcrumb mbx_fl_10">
            <a href="">首页</a>
            <a href="">代理商管理</a>
            <a><cite>销售策略权限列表</cite></a>
        </span>
        <a class="layui-btn layui-btn-small mbx_fr_10" style="line-height:1.6em;margin-top:3px"
           onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
        </a>
    </div>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
{{--                    <div class="layui-card-body ">--}}
{{--                        <form class="layui-form layui-col-space5">--}}
{{--                            <div class="layui-inline layui-show-xs-block">--}}
{{--                                <input type="text" name="goods_name" placeholder="商品名称"--}}
{{--                                       value="{{ request('goods_name') }}" class="layui-input">--}}
{{--                            </div>--}}
{{--                            <div class="layui-inline layui-show-xs-block">--}}
{{--                                <input type="text" name="goods_sn" placeholder="商品编号"--}}
{{--                                       value="{{ request('goods_sn') }}" class="layui-input">--}}
{{--                            </div>--}}
{{--                            <div class="layui-inline layui-show-xs-block">--}}
{{--                                <select name="has_permission">--}}
{{--                                    <option value="">全部权限状态</option>--}}
{{--                                    <option value="1" {{ request('has_permission') == '1' ? 'selected' : '' }}>已有权限</option>--}}
{{--                                    <option value="0" {{ request('has_permission') == '0' ? 'selected' : '' }}>无权限</option>--}}
{{--                                </select>--}}
{{--                            </div>--}}
{{--                            <div class="layui-inline layui-show-xs-block">--}}
{{--                                <button class="layui-btn" lay-submit="" lay-filter="sreach">--}}
{{--                                    <i class="layui-icon">&#xe615;</i>搜索--}}
{{--                                </button>--}}
{{--                            </div>--}}
{{--                        </form>--}}
{{--                    </div>--}}
                    <div class="layui-card-header">
                        <button class="layui-btn" onclick="batchGrant('page')">
                            <i class="layui-icon">&#xe608;</i>授权已选择
                        </button>
                        <button class="layui-btn" onclick="batchGrant('all')">
                            <i class="layui-icon">&#xe608;</i>授予全部权限
                        </button>
                        <button class="layui-btn layui-btn-danger" onclick="batchRevoke('page')">
                            <i class="layui-icon">&#xe640;</i>取消已选择
                        </button>
                        <button class="layui-btn layui-btn-danger" onclick="batchRevoke('all')">
                            <i class="layui-icon">&#xe640;</i>取消全部权限
                        </button>
                    </div>
                    <div class="layui-card-body layui-table-body layui-table-main">
                        <table class="layui-table layui-form">
                            <thead>
                            <tr>
                                <th><input type="checkbox" lay-filter="checkall" name="" lay-skin="primary"></th>
                                <th>ID</th>
                                <th>名称</th>
                                <th>说明</th>
                                <th>状态</th>
                                <th>生效时间</th>
                                <th>失效时间</th>
                                <th>权限状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @forelse($data as $item)
                                <tr class="{{ $item->has_permission ? 'has-permission' : 'no-permission' }}">
                                    <td><input type="checkbox" name="id" value="{{ $item->id }}" lay-skin="primary"></td>
                                    <td>{{ $item->id }}</td>
                                    <td>{{ $item->type_str }}</td>
                                    <td>{{ $item->note }}</td>
                                    <td>
                                        @if($item['status'] == '正常')
                                            <span style="color: #009688">{{$item['status']}}</span>
                                        @else
                                            <span style="color: #FF5722">{{$item['status']}}</span>
                                        @endif
                                    </td>
                                    <td>{{ $item->start }}</td>
                                    <td>{{ $item->end }}</td>
                                    <td>
                                        @if($item->has_permission)
                                            <span class="layui-badge layui-bg-green">已有权限</span>
                                        @else
                                            <span class="layui-badge layui-bg-gray">无权限</span>
                                        @endif
                                    </td>
                                    <td class="td-manage">
                                        @if($item->has_permission)
                                            <a class="layui-btn layui-btn-xs layui-btn-danger" style="margin-top: 5px"
                                               onclick="updatePermission({{ $item->id }}, 0)">
                                                取消权限
                                            </a>
                                        @else
                                            <a class="layui-btn layui-btn-xs" style="margin-top: 5px"
                                               onclick="updatePermission({{ $item->id }}, 1)">
                                                授予权限
                                            </a>
                                        @endif
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="7" class="text-center">暂无数据</td>
                                </tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>
                    <div class="layui-card-body ">
                        <div class="page">
                            {{ $data->appends(['store_id' => $params['store_id'],'type' => $params['type']])->links('admin/public/page') }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        layui.use(['layer', 'form'], function () {
            var layer = layui.layer;
            var form = layui.form;

            // 监听全选
            form.on('checkbox(checkall)', function(data){
                if(data.elem.checked){
                    $('tbody input').prop('checked',true);
                }else{
                    $('tbody input').prop('checked',false);
                }
                form.render('checkbox');
            });

            // 初始化表单元素
            form.render();
        });

        // 更新权限状态
        function updatePermission(goodsId, status) {
            layer.confirm('确认要' + (status ? '授予' : '取消') + '权限吗？', function (index) {
                $.ajax({
                    type: 'POST',
                    url: '/admin/store/updateSalesPermission',
                    data: {
                        goods_id: goodsId,
                        status: status,
                        store_id: {{ $params['store_id'] ?? 0 }},
                        _token: '{{ csrf_token() }}'
                    },
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1}, function () {
                                location.reload();
                            });
                        } else {
                            layer.alert(res.msg);
                        }
                    },
                    error: function () {
                        layer.alert('操作失败，请重试');
                    }
                });
            });
        }

        // 批量授予权限
        function batchGrant(type) {
            var goodsIds = [];
            var storeId = {{ $params['store_id'] ?? 0 }};

            if (type === 'page') {
                // 获取当前页选中的商品ID
                $('tbody input:checked').each(function(){
                    goodsIds.push($(this).val());
                });
            } else if (type === 'all') {
                // 全部授予不需要传商品ID，后台处理
            }

            if (type !== 'all' && goodsIds.length === 0) {
                layer.msg('请至少选择一条记录', {icon: 2});
                return;
            }

            layer.confirm('确认要授予' + (type === 'all' ? '全部' : '选中') + '商品权限吗？', function(index) {
                $.ajax({
                    type: 'POST',
                    url: '/admin/store/batchGrantSalesPermission',
                    data: {
                        goods_ids: goodsIds,
                        type: type,
                        sales_type: {{ $params['type'] }},
                        store_id: storeId,
                        _token: '{{ csrf_token() }}'
                    },
                    success: function(res) {
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1}, function() {
                                location.reload();
                            });
                        } else {
                            layer.alert(res.msg);
                        }
                    },
                    error: function() {
                        layer.alert('操作失败，请重试');
                    }
                });
            });
        }

        // 批量取消权限
        function batchRevoke(type) {
            var goodsIds = [];
            var storeId = {{ $params['store_id'] ?? 0 }};

            if (type === 'page') {
                // 获取当前页选中的商品ID
                $('tbody input:checked').each(function(){
                    goodsIds.push($(this).val());
                });
            } else if (type === 'all') {
                // 全部取消不需要传商品ID，后台处理
            }

            if (type !== 'all' && goodsIds.length === 0) {
                layer.msg('请至少选择一条记录', {icon: 2});
                return;
            }

            layer.confirm('确认要取消' + (type === 'all' ? '全部' : '选中') + '商品权限吗？', function(index) {
                $.ajax({
                    type: 'POST',
                    url: '/admin/store/batchRevokeSalesPermission',
                    data: {
                        goods_ids: goodsIds,
                        type: type,
                        sales_type: {{ $params['type'] }},
                        store_id: storeId,
                        _token: '{{ csrf_token() }}'
                    },
                    success: function(res) {
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1}, function() {
                                location.reload();
                            });
                        } else {
                            layer.alert(res.msg);
                        }
                    },
                    error: function() {
                        layer.alert('操作失败，请重试');
                    }
                });
            });
        }
    </script>
@endsection